package com.itheima.youdianda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.youdianda.domain.YddArticle;
import com.itheima.youdianda.domain.YddCate;
import com.itheima.youdianda.domain.YddUser;
import com.itheima.youdianda.domain.YddUserData;
import com.itheima.youdianda.dto.PageDto;
import com.itheima.youdianda.dto.YddArticleDto;
import com.itheima.youdianda.mapper.YddArticleMapper;
import com.itheima.youdianda.service.YddArticleService;
import com.itheima.youdianda.service.YddCateService;
import com.itheima.youdianda.service.YddUserDataService;
import com.itheima.youdianda.service.YddUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 *文章管理
 */
@Service
@Transactional
@Slf4j
public class YddArticleServiceImpl extends ServiceImpl<YddArticleMapper, YddArticle> implements YddArticleService {
    @Autowired
    private YddCateService yddCateService;

    @Autowired
    private YddUserService yddUserService;

    @Autowired
    private YddUserDataService yddUserDataService;

    /**
     * 文章分页
     * @param page
     * @param title
     * @param limit
     * @param cateid
     * @param start_time
     * @param end_time
     * @return
     */

    @Override
    public PageDto<YddArticleDto> artList(Integer page, String title, Integer limit, Integer cateid, Long start_time, Long end_time) {
        Page<YddArticle> pageInfo = new Page<>(page,limit);
        Page<YddArticleDto> dtoPageInfo = new Page<>();
        //根据条件查询
        LambdaQueryWrapper<YddArticle> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(null!=title,YddArticle::getTitle,title);
        queryWrapper.eq(null!=cateid,YddArticle::getCateid,cateid);
        queryWrapper.ge(null != start_time, YddArticle::getCreateDate, start_time);
        queryWrapper.lt(null != end_time, YddArticle::getCreateDate, end_time);
        queryWrapper.in(YddArticle::getStatus,"0","1","2");
        queryWrapper.orderByDesc(YddArticle::getCreateDate);
        this.page(pageInfo,queryWrapper);
        //将内容copy给dto
        BeanUtils.copyProperties(pageInfo,dtoPageInfo,"records");
        //封装dtoPageInfo中的数据
        List<YddArticle> records = pageInfo.getRecords();
        List<YddArticleDto> dtoList=records.stream().map((item)->{
            YddArticleDto yddArticleDto = new YddArticleDto();
            BeanUtils.copyProperties(item,yddArticleDto);
            YddCate yddCate = yddCateService.getById(item.getCateid());
            String catename = yddCate.getCatename();
            yddArticleDto.setCatename(catename);
            return yddArticleDto;
        }).collect(Collectors.toList());
        dtoPageInfo.setRecords(dtoList);
        PageDto<YddArticleDto> yddArticleDtoPageDto = new PageDto<>();
        BeanUtils.copyProperties(dtoPageInfo,yddArticleDtoPageDto,"records");
        yddArticleDtoPageDto.setValue(dtoPageInfo);
        return yddArticleDtoPageDto;
    }

    /**
     * 删除文章
     * @param id
     */
    @Override
    public void del(Integer id) {
        YddArticle yddArticle = this.getById(id);
        //设置文章状态为3
        yddArticle.setStatus(3);
        //清空该文章点赞收藏信息
        LambdaQueryWrapper<YddUserData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(YddUserData::getArticleId,id);
        List<YddUserData> yddUserDataList = yddUserDataService.list(queryWrapper);
        if (yddUserDataList!=null){
            List<Integer> idList=yddUserDataList.stream().map((item)->{
                Integer dataId = item.getId();
                return dataId;
            }).collect(Collectors.toList());
            yddUserDataService.removeByIds(idList);
        }
        this.updateById(yddArticle);
    }

    /**
     * 添加文章
     * @param yddArticle
     */
    @Override
    public void add(YddArticle yddArticle) {
        //设置userid
        log.info(yddArticle.toString());
        String author = yddArticle.getAuthor();
        LambdaQueryWrapper<YddUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(YddUser::getUsername,author);
        YddUser yddUser = yddUserService.getOne(queryWrapper);
        Integer id = yddUser.getId();
        yddArticle.setUserId(id);
        yddArticle.setCreateDate(yddArticle.getCreateDate());
        this.save(yddArticle);
    }

    /**
     * 修改文章
     * @param yddArticle
     */
    @Override
    public void updateArticle(YddArticle yddArticle) {
        yddArticle.setUpdateDate(System.currentTimeMillis());
        this.updateById(yddArticle);
    }

    /**
     * 文章详情
     * @param id
     * @return
     */
    @Override
    public YddArticle yddArticleInfo(Integer id) {
        YddArticle yddArticle = this.getById(id);
        return yddArticle;
    }
}

